{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2.16 \u4ee5\u6307\u5b9a\u5217\u5bbd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e9b\u957f\u5b57\u7b26\u4e32\uff0c\u60f3\u4ee5\u6307\u5b9a\u7684\u5217\u5bbd\u5c06\u5b83\u4eec\u91cd\u65b0\u683c\u5f0f\u5316\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 textwrap \u6a21\u5757\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u8f93\u51fa\u3002\u6bd4\u5982\uff0c\u5047\u5982\u4f60\u6709\u4e0b\u5217\u7684\u957f\u5b57\u7b26\u4e32\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = \"Look into my eyes, look into my eyes, the eyes, the eyes, \\\nthe eyes, not around the eyes, don't look around the eyes, \\\nlook into my eyes, you're under.\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u6f14\u793a\u4f7f\u7528 textwrap \u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u591a\u79cd\u65b9\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import textwrap\nprint(textwrap.fill(s, 70))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(textwrap.fill(s, 40))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(textwrap.fill(s, 40, initial_indent='    '))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(textwrap.fill(s, 40, subsequent_indent='    '))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "textwrap \u6a21\u5757\u5bf9\u4e8e\u5b57\u7b26\u4e32\u6253\u5370\u662f\u975e\u5e38\u6709\u7528\u7684\uff0c\u7279\u522b\u662f\u5f53\u4f60\u5e0c\u671b\u8f93\u51fa\u81ea\u52a8\u5339\u914d\u7ec8\u7aef\u5927\u5c0f\u7684\u65f6\u5019\u3002\n\u4f60\u53ef\u4ee5\u4f7f\u7528 os.get_terminal_size() \u65b9\u6cd5\u6765\u83b7\u53d6\u7ec8\u7aef\u7684\u5927\u5c0f\u5c3a\u5bf8\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\nos.get_terminal_size().columns"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "fill() \u65b9\u6cd5\u63a5\u53d7\u4e00\u4e9b\u5176\u4ed6\u53ef\u9009\u53c2\u6570\u6765\u63a7\u5236tab\uff0c\u8bed\u53e5\u7ed3\u5c3e\u7b49\u3002\n\u53c2\u9605 textwrap.TextWrapper\u6587\u6863 \u83b7\u53d6\u66f4\u591a\u5185\u5bb9\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}